package wxshop.web.admin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import wxshop.api.admin.model.wx.Attention;
import wxshop.api.admin.model.wx.ReplyTextMess;
import wxshop.api.admin.model.wx.WxUserInfo;
import wxshop.api.user.service.UserService;
import wxshop.web.admin.tools.ConstValues;
import wxshop.web.admin.tools.GetToken;
import wxshop.web.admin.tools.HttpUtil;
import wxshop.web.admin.tools.XmlAndBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by 121874694@qq.com on 2018/3/4.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@RestController
public class WxConnectController {
	//连接微信
	@RequestMapping(value = "/connect",method = RequestMethod.GET)
	public void connect (HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//将拿到的echostr字符串返回去
		resp.getWriter().print(req.getParameter("echostr"));
	}
	//关注事件
	@Autowired
	private UserService userService;
	@RequestMapping(value = "/connect",method = RequestMethod.POST)
	public void attention (HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//设置编码
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		//获取输入流
		String xml;
		InputStream inputStream = req.getInputStream();
		try{
			byte[]bytes = new byte[1024];
			int len = inputStream.read(bytes);
			 xml = new String(bytes,0,len);
		}finally {
			inputStream.close();
		}
		//将获取的json转化为JavaBean,获取用户的openid
		Attention attention = XmlAndBean.toJavaBean(xml,Attention.class);
		System.out.println(attention.getEvent());
		//如果获取的event为关注事件，将向用户推送文本消息
		if (attention.getEvent().equals("subscribe")){
			ReplyTextMess replyTextMess = new ReplyTextMess();
			replyTextMess.setToUserName(attention.getFromUserName());
			replyTextMess.setFromUserName(attention.getToUserName());
			replyTextMess.setMsgType("text");
			replyTextMess.setContent(ConstValues.REPLYTEXT);
			replyTextMess.getCreateTime(System.currentTimeMillis());
			//将JavaBean转化为json字符串
			resp.getWriter().print(XmlAndBean.toXml(replyTextMess, ConstValues.CHARSET));
		}
		//通过用户的openid拿到用户的基本信息
		GetToken.getToken();
		String url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ConstValues.TOKEN+"&openid="+attention.getFromUserName()+"&lang=zh_CN";
		String a =HttpUtil.doGet(url2,"UTF8");
		String json = new String(a.getBytes("ISO-8859-1"),"UTF-8");
		ObjectMapper objectMapper = new ObjectMapper();
		WxUserInfo wxUserInfo = objectMapper.readValue(json, WxUserInfo.class);
		//关注获取用户基本信息插入数据库
		if(attention.getEvent().equals("subscribe")){
			userService.insertUserInfo(wxUserInfo);
		}
		//取消关注删除数据库信息，优化可以做状态避免重复插入删除
		if(attention.getEvent().equals("unsubscribe")){
			userService.deleteUserInfo(attention.getFromUserName());
		}
		
	}
	
}
